package cn.lf.android.lawfirm.utils.timepicker;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.ArrayRes;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

import cn.lf.android.lawfirm.R;

/**
 * 时间选择POPWindow,利用{@link ChooseTime} 实现,只做了UI的处理
 * Created by YZL on 2017/6/24.
 */
public class TimePicker extends PopupWindow implements View.OnClickListener {
    private Context mContext;
    private OnPickerCallBack callBack;
    private ChooseTime mChooseTime;
    private LinearLayout timeViewContantor;
    private View contentV;
    private View dataPick;

    /**
     * 日期选择
     *
     * @param context
     * @param dimensionNumber 精确到时,为4
     * @param callBack
     */
    public TimePicker(Context context, int dimensionNumber, OnPickerCallBack callBack) {
        super(context);
        this.mContext = context;
        this.callBack = callBack;
        mChooseTime = new ChooseTime(context, this);
        mChooseTime.SetDimensionNumber(dimensionNumber);
        initPopWindow();
    }


    /**
     * 日期选择
     *
     * @param context
     * @param dimensionNumber 精确到时,为4
     * @param checkTime 是否校验时间(true,时间在当前时间之前不可选取)
     * @param callBack
     */
    public TimePicker(Context context, int dimensionNumber,boolean checkTime, OnPickerCallBack callBack) {
        super(context);
        this.mContext = context;
        this.callBack = callBack;
        mChooseTime = new ChooseTime(context, this);
        mChooseTime.SetDimensionNumber(dimensionNumber);
        mChooseTime.setCheckTime(checkTime);
        initPopWindow();
    }

    /**
     * 单列选择
     *
     * @param context
     * @param dimensionNumber 传1
     * @param itemData
     * @param callBack
     */
    public TimePicker(Context context, int dimensionNumber, List<String> itemData, OnPickerCallBack callBack) {
        super(context);
        this.mContext = context;
        this.callBack = callBack;
        mChooseTime = new ChooseTime(context, this);
        mChooseTime.SetDimensionNumber(dimensionNumber);
        mChooseTime.SetList(itemData);
        initPopWindow();
    }


    /**
     * 单列选择
     *
     * @param context
     * @param dimensionNumber 传1
     * @param id 数组资源ID
     * @param callBack
     */
    public TimePicker(Context context, int dimensionNumber, @ArrayRes int id, OnPickerCallBack callBack) {
        super(context);
        this.mContext = context;
        this.callBack = callBack;
        mChooseTime = new ChooseTime(context, this);
        mChooseTime.SetDimensionNumber(dimensionNumber);
        String[] stringArray = context.getResources().getStringArray(id);
        List<String> itemData= Arrays.asList(stringArray);
        mChooseTime.SetList(itemData);
        initPopWindow();
    }

    private void initPopWindow() {
        contentV = LayoutInflater.from(mContext).inflate(R.layout.pop_time_picker, null);
        timeViewContantor = (LinearLayout) contentV.findViewById(R.id.timepick);
        dataPick = mChooseTime.getDataPick(this);
        timeViewContantor.addView(dataPick);
        View maskV = dataPick.findViewById(R.id.mask);
        maskV.setOnClickListener(this);
        if (mChooseTime.getDimensionnumber() == 1) {
//            maskV.setBackgroundResource(R.mipmap.white_bg);
        }
        setContentView(contentV);

        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        setFocusable(true);
        setOutsideTouchable(true);
        setAnimationStyle(R.style.PickerViewAnimStyle);
        update();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.done:
                if (mChooseTime.getDimensionnumber() == 1) {
                    callBack.pickerDone(mChooseTime.getTxt(), mChooseTime.getYear().getCurrentItem());
                    dismiss();
                } else {
                    if (mChooseTime.compareDate()) {
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTimeInMillis(0);
                        calendar.set(mChooseTime.getYear().getCurrentItem() + 1950,
                                mChooseTime.getMonth().getCurrentItem(),
                                mChooseTime.getDay().getCurrentItem() + 1
                                , mChooseTime.getDimensionnumber() == 4 ?
                                        mChooseTime.getHour().getCurrentItem() : 0,
                                0, 0);
                        callBack.pickerDone(mChooseTime.getTxt(), calendar.getTimeInMillis());
                        dismiss();
                    }
                }
                break;
            case R.id.mask:
                dismiss();
                break;
        }
    }

    /**
     * 值可以传入 decorView
     *
     * @param parent
     */
    public void show(View parent) {
        if (isShowing()) {
            return;
        }
        showAtLocation(parent, Gravity.NO_GRAVITY, 0, 0);
    }

//    /**
//     * {@link ChooseTime#SetDimensionNumber(int)}
//     *
//     * @param number
//     */
//    public void setDimensionNumber(int number) {
//        mChooseTime.SetDimensionNumber(number);
//    }

    public ChooseTime getmChooseTime() {
        return mChooseTime;
    }

//    public void setChooseTime(ChooseTime chooseTime) {
//        this.mChooseTime = chooseTime;
//        if (timeViewContantor != null) {
//            timeViewContantor.removeAllViews();
//            timeViewContantor.addView(chooseTime.getDataPick(this));
//        }
//    }

    public interface OnPickerCallBack {
        /**
         * 如果是ChooseTime 为1 ,第二个参数返回index,第一个参数返回文本
         *
         * @param timeString   时间字符串
         * @param timeInMillis 时间戳
         */
        void pickerDone(String timeString, long timeInMillis);
    }

}
